<?php
namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Mockery\Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\RequestBackController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CheckController;
/*
   客户名录
*/
class CustomerCenterController extends Controller{
   
	 /*
        客户名录显示
    */
	public function ShowCustomer($request){
        if(!CheckController::CheckUser($request)){
             return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $page = isset($pos['page']) ? $pos['page']  : 1;
        $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
        $offset = ($page - 1) * $pageSize;
        // print_r($offset);die();
        if(!CheckController::CheckParameter($pos,['UserId','key'])){//
           return RequestBackController::Structure(502);
        }else{
             $Purview = new PurviewController();
             $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
             if($UserPurview == 'All') {//超级管理员显示所有客户名单
                 $Customer = DB::connection('mysql_other')->table('yl_litigants as l')->select('l.litigant_id_card as LitigantIdCard','l.litigant_id as LitigantId','l.case_id as CaseId','l.litigation_status_id as LitigationStatusId', 'l.litigant_name as LitigantName','l.litigant_mobile as LitigantMobile','l.litigant_able as LitigantAble','s.litigation_status_id as LitigationStatusId','s.litigation_status_name as LitigationStatusName','t.litigant_type_id as LitigantTypeId','t.litigant_type_title as LitigantTypeTitle','l.litigant_type_id as LitigantTypeId')
                            ->join("yl_litigation_status as s",function($join){
                                $join->on('l.litigation_status_id','s.litigation_status_id');
                            })
                            ->join("yl_litigant_type as t",function($join){
                                $join->on('t.litigant_type_id','l.litigant_type_id');
                            })
							->where('l.litigant_name','like','%'.$pos['key'].'%')->orWhere('l.litigant_mobile','like','%'.$pos['key'].'%')->orWhere('l.litigant_id_card','like','%'.$pos['key'].'%');
                 $count = $Customer->count();
                 $Customer = $Customer->offset($offset)->limit($pageSize)->orderBy('l.litigant_id','desc')
                     ->get()->map(function ($val) {
                         $val = (array)$val;
                         return $val;
                     })
                    ->keyBy(function($item)
                    {
                        return $item['LitigantId'];
                    })
                     ->toArray();
				$CaseInfo = DB::connection('mysql_other')->table('yl_case')->select('case_id as CaseId','case_title as CaseTitle')
							->get()->map(function ($val) {
							 $val = (array)$val;
							 return $val;
							})
							->keyBy(function($item)
							{
								return $item['CaseId'];
							})
							 ->toArray();
					// print_r($Customer);die();
                 $sum_page = ceil($count / $pageSize);  //总页码
                 if ($page > $sum_page && $sum_page > 0) {
                     return RequestBackController::Structure(301);
                 } else {
                     if ($Customer) {
						 //print_r($Customer);die();
						 foreach ($Customer as $k =>$v) {
                            if(isset($v['CaseId'])){
								
								$Customer[$k]['CaseTitle']=isset($CaseInfo[$v['CaseId']]['CaseTitle'])?$CaseInfo[$v['CaseId']]['CaseTitle']:'';
                        
                            }else{
								$Customer[$k]['CaseTitle'] ="";
							}
						}
						 $Customers['List'] = $Customer;
                         $Customers['count'] = $count;
                         return RequestBackController::Structure(501,$Customers);
                     } else {
                         return RequestBackController::Structure(504);
                     }
                 }
             }
        }
    }
	/*
    新建客户
    */
    public function AddCustomer($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','LitigantName','LitigantMobile','LitigantIdCard','LitigationStatusId','LitigantAble'])){
            return RequestBackController::Structure(502);
        }else{
                $add=array();
                //$add['case_id'] = $pos['CaseId'];//
                $add['litigation_status_id'] =$pos['LitigationStatusId'];//诉讼地位
                $add['litigant_name'] = $pos['LitigantName'];//姓名
                $add['litigant_mobile'] = $pos['LitigantMobile'];//电话
				$add['litigant_id_card'] = $pos['LitigantIdCard'];//身份证号
				$add['litigant_able'] =$pos['LitigantAble'];//在办理任务
                $row = DB::connection('mysql_other')->table('yl_litigants')->insert($add);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

        }


    }
	   /*
    编辑客户
    */
    public function EditCustomer($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','LitigantId','LitigantName','LitigantMobile','LitigantIdCard','LitigationStatusId','LitigantAble'])){
            return RequestBackController::Structure(502);
        }else{
            $isRow = DB::connection('mysql_other')->table('yl_litigants')->where('litigant_id',$pos['LitigantId'])->first();
            if($isRow->case_id){//有案件不允许编辑
                return RequestBackController::Structure(606);
            }else{
                $map=array();
                //$map['case_id'] = $pos['CaseId'];//
                //$map['litigation_status_id'] =$pos['LitigationStatusId'];//诉讼地位
                $map['litigant_name'] = $pos['LitigantName'];//姓名
                $map['litigant_mobile'] = $pos['LitigantMobile'];//电话
				$map['litigant_id_card'] = $pos['LitigantIdCard'];//身份证号
				$map['litigant_able'] =$pos['LitigantAble'];//在办理任务
				$map['litigation_status_id'] =$pos['LitigationStatusId'];//诉讼地位
                $row = DB::connection('mysql_other')->table('yl_litigants')->where('litigant_id',$pos['LitigantId'])->update($map);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }
            

        }


    }
	 /*
    客户是否在办
    */
    public function AbleCustomer($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','LitigantId','LitigantAble'])){
            return RequestBackController::Structure(502);
        }else{
                $map=array();
                $map['litigant_able'] =$pos['LitigantAble'];
                $row = DB::connection('mysql_other')->table('yl_litigants')->where('litigant_id',$pos['LitigantId'])->update($map);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

        }


    }
	/*
    删除客户
    */
    public function DeleteCustomer($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','LitigantId'])){
            return RequestBackController::Structure(502);
        }else{  
            //查询该用户是否有关联案件
            $isRow = DB::connection('mysql_other')->table('yl_litigants')->where('litigant_id',$pos['LitigantId'])->first();
            if($isRow->case_id){//有案件不允许删除
                return RequestBackController::Structure(606);
            }else{
                $row = DB::connection('mysql_other')->table('yl_litigants')->where('litigant_id',$pos['LitigantId'])->delete();
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }
               

        }


    }
	
	  /*
    文件上传
    */
    public function upload_file($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205); 
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','file'])){
            return RequestBackController::Structure(502);
        }else{
       $file=$_FILES['file']; //获取上传的文件//upfile <input type="file" name="upfile" />
	   $fileName = $pos['file'];
	   $originalName = $fileName->getClientOriginalName();//原文件名
            //存在该文件夹时直接新建文件
			$file=$_FILES['file'];
			if(is_uploaded_file($file['tmp_name'])){//判断是否通过post传递
			$num = strtotime("now")+mt_rand(99,999);
			   //获取上传文件的后缀名
				$str = substr($file['name'], strrpos($file['name'], '.')+1);
				$path="ApproveMode/".$num.'.'.$str;   // 定义上传文件的存储位置
				if(move_uploaded_file($file['tmp_name'],$path)){   // 执行文件上传操作
					$data['name'] =$originalName;
					$data['path'] =$path;
					return RequestBackController::Structure(501,$data);//将文件地址返回给前端（可在此操作加上保存进数据库）
				}else{
					return RequestBackController::Structure(502);
				}
			}
        }
    }
	
	/*
    诉讼地位列表
    */
    public function LitigationStatus($request){
        if (!CheckController::CheckUser($request)) {
            return RequestBackController::Structure(205);
        }
        $pos = $request->all();
        $StatusInfo = DB::connection('mysql_other')->table('yl_litigation_status as c')->select('c.litigation_status_id as LitigationStatusId','c.litigation_status_name as LitigationStatusName')
		->where('c.litigation_status_id','>',0)
                    ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
        if($StatusInfo){
            return RequestBackController::Structure(501,$StatusInfo);
        }else{
            return RequestBackController::Structure(504);
        }

    }
    







   
}
